import {
    querymyinterviews,
    queryleaderinterviews,
    queryallinterviews,
    getinterviewrecruitinfo,
    savefistinterview,
    getinterviewinfo,
    updateinterviewarrange,
    fetchInterviewRecord,
    saveinterviewrecord,
    featchApply,
    getinterviewimage
} from '@/services/myRecruit/interview';
import { getDict, querylist } from '@/services/myRecruit/talentPool';

export default {
    namespace: 'interview',
    state: {
        querymyinterviewsList: [],
        queryleaderinterviewsList: [],
        queryallinterviewsList: [],
        querylistList: [],
        interviewRecordList: [],
        interviewTypeList: [], // 面试类型字典
        interviewConclusionTypeList: [], // 面试结果字典
        pagination: {
            pageSize: 10,
            current: 1,
            total: 0
        },
        conductpagination: {
            pageSize: 10,
            current: 1,
            total: 0
        },
        adminpagination: {
            pageSize: 10,
            current: 1,
            total: 0
        },
        orderBy: ''
    },
    effects: {
        // 我发起的面试
        *querymyinterviews({ payload }, { call, put }) {
            const result = yield call(querymyinterviews, payload);
            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        querymyinterviewsList: result.data.rows,
                        pagination: {
                            current: result.data.pageNo,
                            pageSize: result.data.pageSize,
                            total: result.data.total
                        }
                    }
                });
            }
        },

        // 我进行的面试
        *queryleaderinterviews({ payload }, { call, put }) {
            const result = yield call(queryleaderinterviews, payload);
            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        queryleaderinterviewsList: result.data.rows,
                        conductpagination: {
                            current: result.data.pageNo,
                            pageSize: result.data.pageSize,
                            total: result.data.total
                        }
                    }
                });
            }
        },
        // 领导层查看面试记录
        *queryallinterviews({ payload }, { call, put }) {
            const result = yield call(queryallinterviews, payload);
            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        queryallinterviewsList: result.data.rows,
                        adminpagination: {
                            current: result.data.pageNo,
                            pageSize: result.data.pageSize,
                            total: result.data.total
                        }
                    }
                });
            }
        },
        // 查询应聘职位下拉
        *querylist({ payload, callback }, { call, put }) {
            const result = yield call(querylist, payload);
            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        querylistList: result.data
                    }
                });
                if (callback) callback();
            }
        },

        //  面试管理-通过候选人arrangeCode获取应聘者信息
        *getinterviewrecruitinfo({ payload, callback }, { call }) {
            const result = yield call(getinterviewrecruitinfo, payload);
            if (result && result.code === 10000) {
                if (callback) callback(result.data);
            }
        },
        //  面试管理-通过候选人candidateCode获取面试安排
        *getinterviewinfo({ payload, callback }, { call }) {
            const result = yield call(getinterviewinfo, payload);
            if (result && result.code === 10000) {
                if (callback) callback(result.data);
            }
        },

        //  发起面试
        *savefistinterview({ payload, callback }, { call }) {
            const result = yield call(savefistinterview, payload);
            if (callback) callback(result);
        },
        //  修改面试安排
        *updateinterviewarrange({ payload, callback }, { call }) {
            const result = yield call(updateinterviewarrange, payload);
            if (callback) callback(result);
        },
        //  保存面试结果
        *saveinterviewrecord({ payload, callback }, { call }) {
            const result = yield call(saveinterviewrecord, payload);
            if (callback) callback(result);
        },
        *fetchInterviewRecord({ payload }, { call, put }) {
            const result = yield call(fetchInterviewRecord, payload);
            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        interviewRecordList: result.data
                    }
                });
            }
        },
        *getListDict(_, { call, put, all }) {
            const [interviewType, interviewConclusionType] = yield all([
                call(getDict, { code: 'interviewType' }),
                call(getDict, { code: 'interviewConclusionType' })
            ]);
            if (interviewType.code === 10000) {
                const List = [];
                interviewType.data.map(item =>
                    List.push({
                        text: item.dictName,
                        value: item.dictValue
                    })
                );
                yield put({
                    type: 'save',
                    payload: {
                        interviewTypeList: List
                    }
                });
            }
            if (interviewConclusionType.code === 10000) {
                const List = [];
                interviewConclusionType.data.map(item =>
                    List.push({
                        text: item.dictName,
                        value: item.dictValue
                    })
                );
                yield put({
                    type: 'save',
                    payload: {
                        interviewConclusionTypeList: List
                    }
                });
            }
        },
        // 发起offer申请
        *featchApply({ payload, callback }, { call }) {
            const result = yield call(featchApply, payload);
            if (result.code === 10000) {
                if (callback) callback(result.data);
            }
        },
        // 获取面试评分表
        *getinterviewimage({ payload, callback }, { call }) {
            const result = yield call(getinterviewimage, payload);
            if (result.code === 10000) {
                if (callback) callback(result.data);
            }
        }
    },
    reducers: {
        save(state, { payload }) {
            return {
                ...state,
                ...payload
            };
        }
    }
};
